{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Most examples work across multiple plotting backends, this example is also available for:\n",
    "\n",
    "* [Bokeh NYC Taxi Connection](../bokeh/nyc_taxi_connections.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import networkx as nx\n",
    "import holoviews as hv\n",
    "\n",
    "from holoviews import dim, opts\n",
    "from holoviews.element.graphs import layout_nodes\n",
    "from bokeh.sampledata.airport_routes import routes, airports\n",
    "\n",
    "hv.extension('matplotlib')\n",
    "hv.output(fig='svg', size=300)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Declare data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create dataset indexed by AirportID and with additional value dimension\n",
    "airports = hv.Dataset(airports, ['AirportID'], ['Name', 'IATA', 'City'])\n",
    "source_airports = list(airports.select(City='New York').data.AirportID)\n",
    "\n",
    "# Add connections count to routes then aggregate and select just routes connecting to NYC\n",
    "routes['connections'] = 1\n",
    "nyc_graph = hv.Graph((routes, airports), ['SourceID', \"DestinationID\"], ['connections'], label='NYC Airport Connections')\\\n",
    "    .aggregate(function=np.count_nonzero).select(SourceID=source_airports)\n",
    "\n",
    "# Lay out graph weighting and weight by the number of connections\n",
    "np.random.seed(14)\n",
    "graph = layout_nodes(nyc_graph, layout=nx.layout.fruchterman_reingold_layout, kwargs={'weight': 'connections'})\n",
    "labels = hv.Labels(graph.nodes, ['x', 'y'], ['IATA', 'City'])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nyc_labels = labels.select(City='New York').opts(\n",
    "    color='white', yoffset=0.05, size=16)\n",
    "\n",
    "other_labels = labels[labels['City']!='New York'].opts(\n",
    "    color='white', size=8)\n",
    "\n",
    "cmap = {3697: 'red', 3797: 'blue'}\n",
    "\n",
    "(graph * nyc_labels * other_labels).opts(\n",
    "    opts.Graph(\n",
    "        bgcolor='gray', xaxis=None, yaxis=None, padding=0.1,\n",
    "        edge_color=dim('SourceID').categorize(cmap, 'gray'),\n",
    "        node_color=dim('index').categorize(cmap, 'gray'),\n",
    "        title='NYC Airport Connections')\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
